//
//  SwitchViewController.m
//  iSubinitize
//
//  Created by Ryan Steier on 10/9/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SwitchViewController.h"
#import "WelcomeViewController.h"
#import "SelectionViewController.h"
#import "SettingsViewController.h"


@implementation SwitchViewController
@synthesize welcomeViewController;
@synthesize selectionViewController;
@synthesize settingsViewController;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [settingsViewController release];
    [selectionViewController release];
    [welcomeViewController release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
    if(self.welcomeViewController.view.superview == nil)
        self.welcomeViewController = nil;
    else if (self.settingsViewController.view.superview == nil)
        self.settingsViewController = nil;
    else
        self.selectionViewController = nil;
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView{}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    WelcomeViewController* welcomeController = [[WelcomeViewController alloc] initWithNibName:@"WelcomeView.xib" bundle:nil];
    self.welcomeViewController = welcomeController;
    [self.view insertSubview:welcomeController.view atIndex:0];
    [welcomeController release];
    [super viewDidLoad];
}

- (IBAction)switchViews_selection:(id)sender
{
    if(self.selectionViewController.view.superview != nil)  //currently on selectionViewCont, want to get back to welcomeViewCont
    {
        if(true)//deterimine if the number of selections is done (can move onto screen?
        {//session completed
        
        
        //Although we created the yellowViewController in the viewDidLoad method, it may have been flushed due to memory contraints.  Being a good memory citizen here.
        //Creating the selectionViewController obj for the first time
            if (self.welcomeViewController == nil)
            {
                WelcomeViewController* welcomeController = [[WelcomeViewController alloc] initWithNibName:@"WelcomeView" bundle:nil];
                self.welcomeViewController = welcomeController;
                [welcomeController release];
            }
            //removing welcomeViewCont, and adding the selectionViewCont obj
            [selectionViewController.view removeFromSuperview];
            [self.view insertSubview:welcomeViewController.view atIndex:0];
            //reset the number of selections in this session?
            //other reset variables?
        }else
        {//need to stay on screen, session not over yet
            //increment total number of selections
            //keep track of score
            //etc
        }
    }
    else if (self.welcomeViewController.view.superview != nil) //currently on welcomeViewCont, want to switch to selectionViewCont
    {
        if (self.selectionViewController == nil)
        {
            SelectionViewController* selectionController = [[SelectionViewController alloc] initWithNibName:@"SelectionView" bundle:nil];
            self.selectionViewController = selectionController;
            [selectionController release];
        }
        //removing welcomeViewCont, and adding the selectionViewCont obj
        [welcomeViewController.view removeFromSuperview];
        [self.view insertSubview:selectionViewController.view atIndex:0];
    }
    else
    { NSLog(@"ERROR: SwitchViewController.m-switchViews_selection: no compatible view present");
    }
}



-(IBAction)switchViews_settings:(id)sender
{
    if(self.welcomeViewController.view.superview != nil) //currently on welcomeViewCont, want to switch to settingsViewCont
    {
        if (self.settingsViewController == nil)
        {
            SettingsViewController* settingsController = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil];
            self.settingsViewController = settingsController;
            [settingsController release];
        }
    }
    else if (self.settingsViewController.view.superview != nil) //currently on settingsViewCont, want to switch to welcomeViewCont
    {
        if (self.welcomeViewController == nil)
        {
            WelcomeViewController* welcomeController = [[WelcomeViewController alloc] initWithNibName:@"WelcomeView" bundle:nil];
            self.welcomeViewController = welcomeController;
            [welcomeController release];
        }
        //removing welcomeViewCont, and adding the selectionViewCont obj
        [settingsViewController.view removeFromSuperview];
        [self.view insertSubview:welcomeViewController.view atIndex:0];
    }
    else
    { NSLog(@"ERROR: SwitchViewController.m-switchViews_settings: no compatible view present");
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}


@end
